package com.hivekion.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hivekion.common.entity.PagedResultVo;
import com.hivekion.system.domain.SysOperationLog;
import com.hivekion.system.domain.vo.operationlog.SysOperationLogModelVo;
import com.hivekion.system.domain.vo.operationlog.SysOperationLogSearchPageInputVo;
import com.hivekion.system.domain.vo.operationlog.SysOperationLogViewVo;
import com.hivekion.system.mapper.SysOperationLogMapper;
import com.hivekion.system.service.ISysOperationLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysOperationLogServiceImpl extends ServiceImpl<SysOperationLogMapper, SysOperationLog>
        implements ISysOperationLogService {
    @Autowired
    SysOperationLogMapper mapper;

    @Override
    public void insertOperationLog(SysOperationLog log) {
        this.save(log);
    }

    @Override
    public boolean remove(String operId) {
        return this.removeById(operId);
    }

    @Override
    public void cleanOperationLog() {
        mapper.cleanOperationLog();
    }

    @Override
    public PagedResultVo<SysOperationLogViewVo> list(SysOperationLogSearchPageInputVo inputVo) {
        LambdaQueryWrapper<SysOperationLog> logLambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(inputVo.getFilter())) {
            logLambdaQueryWrapper.and(
                    t ->
                            t.like(SysOperationLog::getMethod, inputVo.getFilter())
                                    .or()
                                    .like(SysOperationLog::getTitle, inputVo.getFilter()));
        }
        if (inputVo.getStartTime() != null) {
            logLambdaQueryWrapper.and(
                    t -> t.gt(SysOperationLog::getCreateTime, inputVo.getStartTime()));
        }
        if (inputVo.getEndTime() != null) {
            logLambdaQueryWrapper.and(
                    t -> t.lt(SysOperationLog::getCreateTime, inputVo.getEndTime()));
        }
        if (inputVo.getLevel() != null && inputVo.getLevel() != -1) {
            logLambdaQueryWrapper.and(t -> t.eq(SysOperationLog::getLevel, inputVo.getLevel()));
        }
        if (inputVo.getStatus() != null && inputVo.getStatus() != -1) {
            logLambdaQueryWrapper.and(t -> t.eq(SysOperationLog::getStatus, inputVo.getStatus()));
        }
        Page<SysOperationLog> logPage = new Page<>(inputVo.getPageNum(), inputVo.getPageSize());
        logPage.addOrder(new OrderItem("create_time", false));
        IPage<SysOperationLog> logPageResult = this.page(logPage, logLambdaQueryWrapper);
        List<SysOperationLogViewVo> data =
                logPageResult.getRecords().stream()
                        .map(
                                item -> {
                                    SysOperationLogViewVo vo = new SysOperationLogViewVo();
                                    BeanUtils.copyProperties(item, vo);
                                    return vo;
                                })
                        .collect(Collectors.toList());
        PagedResultVo<SysOperationLogViewVo> resultVo =
                new PagedResultVo<SysOperationLogViewVo>(
                        inputVo, (int) logPageResult.getTotal(), data);
        return resultVo;
    }

    @Override
    public SysOperationLogModelVo getInfo(String id) {
        SysOperationLog info = this.getById(id);
        SysOperationLogModelVo vo = new SysOperationLogModelVo();
        BeanUtils.copyProperties(info, vo);
        return vo;
    }
}
